//
//  UserWebService.m
//  eGiftr
//
//  Created by Phien Tram on 9/3/13.
//  Copyright (c) 2013 eGiftr. All rights reserved.
//

#import "UserWebService.h"
#import "ServiceNameMapper.h"
#import "NSDictionary+Utility.h"
#import "SBJson.h"

@implementation UserWebService

- (id)initUserRegister:(id)delegate userName:(NSString *)userName email:(NSString *)email password:(NSString *)password city:(NSString *)city fbId:(NSString *)fbId fbToken:(NSString *)fbToken isFbUser:(BOOL)isFbUser
{
    NSDictionary *params = [NSDictionary dictionary];
    if (isFbUser) {
        params = [NSDictionary dictionaryWithObjectsAndKeys:
                  [NSNumber numberWithBool:YES], @"isFbUser",
                  userName, @"login",
                  @"", @"password",
                  fbId, @"fbId",
                  fbToken, @"fbToken",
                  nil];
    } else {
        params = [NSDictionary dictionaryWithObjectsAndKeys:
                  [NSNumber numberWithBool:NO], @"isFbUser",
                  userName, @"login",
                  password, @"password",
                  city, @"city",
                  email, @"email",
                  nil];
    }
    
    self = [super initWithData:params serviceName:[ServiceNameMapper userRegister] method:@"POST"];
    if (self) {
        self.delegate = delegate;
        
        [self request];
    }
    return self;
}

+ (id)userRegister:(id)delegate userName:(NSString *)userName email:(NSString *)email password:(NSString *)password city:(NSString *)city fbId:(NSString *)fbId fbToken:(NSString *)fbToken isFbUser:(BOOL)isFbUser
{
    return [[self alloc] initUserRegister:delegate userName:userName email:email password:password city:city fbId:fbId fbToken:fbToken isFbUser:isFbUser];
}

- (id)initLogin:(id)delegate isFbUser:(NSNumber *)isFbUser login:(NSString *)username password:(NSString *)password
{
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            isFbUser, @"isFbUser",
                            username, @"login",
                            password, @"password",
                            nil];
    self = [super initWithData:params serviceName:[ServiceNameMapper login] method:@"POST"];
    if (self) {
        self.delegate = delegate;
        
        [self request];
    }
    return self;
}

+ (id)login:(id)delegate isFbUser:(NSNumber *)isFbUser login:(NSString *)username password:(NSString *)password
{
    return [[self alloc] initLogin:delegate isFbUser:isFbUser login:username password:password];
}

- (id)initLogout:(id)delegate userId:(NSString *)userId
{
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            userId, @"userId",
                            nil];
    self = [super initWithData:params serviceName:[ServiceNameMapper logout] method:@"POST"];
    if (self) {
        self.delegate = delegate;
        
        [self request];
    }
    return self;
}

+ (id)logout:(id)delegate userId:(NSString *)userId
{
    return [[self alloc] initLogout:delegate userId:userId];
}

- (id)initUpdateFbFriend:(id)delegate fbId:(NSString *)fbId fbToken:(NSString *)fbToken userId:(NSString *)userId appToken:(NSString *)appToken
{
    NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
                          userId, @"id",
                          fbId, @"fbId",
                          fbToken, @"fbToken",
                          nil];
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            appToken, @"appToken",
                            data, @"data",
                            nil];
    self = [super initWithData:params serviceName:[ServiceNameMapper updateFbFriend] method:@"POST"];
    if (self) {
        self.delegate = delegate;
        
        [self request];
    }
    return self;
}

+ (id)updateFbFriend:(id)delegate fbId:(NSString *)fbId fbToken:(NSString *)fbToken userId:(NSString *)userId appToken:(NSString *)appToken
{
    return [[self alloc] initUpdateFbFriend:delegate fbId:fbId fbToken:fbToken userId:userId appToken:appToken];
}

- (BOOL)parse:(NSString **)error code:(NSInteger *)code
{
	id jResult = [self.receivedData JSONValue];
    NSLog(@"jResult: %@", [jResult description]);
    BOOL success = [[jResult objectForKey:@"success"] boolValue];
    if (success)
    {
        error = nil;
        NSDictionary *dataDict = [jResult objectOrNilForKey:@"data"];
        self.result = dataDict;
    }
    else
    {
        NSDictionary *errorDictionary = [jResult objectOrNilForKey:@"error"];
        NSString *message = [errorDictionary objectOrNilForKey:@"errorMessage"];
        NSString *errorDetails = [errorDictionary objectOrNilForKey:@"details"];
        if (message) {
            *error = message;
        } else if (errorDetails) {
            *error = errorDetails;
        }
        NSNumber *errorCode = [errorDictionary objectOrNilForKey:@"errorCode"];
        *code = [errorCode integerValue];
        return NO;
    }
    
    
	return YES;
}

@end
